public class MethodDemo10 {
    public static void main(String[] args) {
        // 求1-5的和
        int sum = 0;
        for (int i = 0; i <= 5; i++) {
            sum += i;
        }
        System.out.println(sum);
        System.out.println(getSum(5));
    }

    /*
        5+(1-4的和)
        1-4的和 = 4+(1-3的和)
        (1-3的和) = 3+(1-2的和)
        (1-2的和) = 2+(1-0的和)
        (1-0的和) =1
          5+4+3+2+1
     */
    public static int getSum(int i) {
        if (i == 1) {
            return 1;
        }
        return i + getSum(i - 1);
    }

    /*
        5+getSum(4)
        getSum(4) = 4+ getSum(3)
        getSum(3) = 3+getSum(2)
        getSum(2) = 2+getSum(1)
        getSum(1) =1
     */


    // 用来返回1-4的和
    public static int getSum1(int i) {
        return 4 + getSum2(3);
    }

    // 用来返回1-3的和
    public static int getSum2(int i) {
        return 3 + getSum3(2);
    }

    // 用来返回1-2的和
    public static int getSum3(int i) {
        return 2 + getSum4(1);
    }

    // 用来返回1-2的和
    public static int getSum4(int i) {
        return 1;
    }
}


